// pages/open_per/tmppwd_share/tmppwd_share.js

var aliyun_api = require('../../../utils/aliyun-api-gateway.js');
var util = require('../../../utils/util.js');
var ble = require('../../../utils/bluetooth.js');
var event = require('../../../utils/event.js');

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startTime:[],
    endTime:[],
    nowTime:[],
    isoverdue:true,
    deviceName:null,
    deviceNick:null,
    ifBleConnected:false,
    bOpenLock: true,
    nowTime: null,
    pass:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    
    var startTime= options.startTime.split(",")
    var endTime= options.endTime.split(",")
    startTime.forEach(item => {
      that.data.startTime.push(+item);
    });
    endTime.forEach(item => {
      that.data.endTime.push(+item);
    });
    // console.log(that.data.startTime)
    // console.log(that.data.endTime)

    that.setData({
      startTime: that.data.startTime,
      endTime: that.data.endTime,
      deviceName: options.deviceName,
      deviceNick: options.deviceNick,
      pass: options.pass,
    })

    //获取标准时间
    // that.getTime();
    
    // event.$on('BLE-connected-event', (sts) => {
    //   that.data.ifBleConnected = sts
    // })


  //  ble.MsgResp()
    event.$on('ready-event', (data) => {
      that.data.bOpenLock = true
    })
    event.$on('send-open-event-tmp', async function(){
      var startTime = that.timeTurn(that.data.startTime)
      var endTime = that.timeTurn(that.data.endTime)
      console.log(startTime, endTime, that.data.pass)
      var content = {
        bTime: startTime,
        eTime: endTime,
        secret: that.data.pass
      }
      var result = await ble.sendMsg(0x07, null, content, null)
      if (result == 7) {
        event.$fire('open-success-event')
      } else if (result == -1) {
        event.$fire('open-fail-event')
      }
    })

    event.$on('open-success-event', (data) => {
        wx.showToast({
          title: '开门成功',
          icon: 'success',
          duration: 1000
        })
        that.data.bOpenLock = true;
    })

    event.$on('open-fail-event', (data) => {
      wx.showToast({
        title: '开门失败',
        duration: 1000,
        image: '/images/fail.png'
      })
      that.data.bOpenLock = true;
    })


  },
  /* 获取标准时间 */
  getTime:function(){
    var that = this;
    return new Promise(function (resolve, reject){
      wx.request({
        url: 'https://www.baidu.com',
        method: 'GET',
        success(res) {
          console.log(res.header.Date)
          var date = new Date(res.header.Date);
          that.data.nowTime = util.formatTime1(date)

          var startTime = that.timeTurn(that.data.startTime)
          var nowTime = that.timeTurn(that.data.nowTime)
          var endTime = that.timeTurn(that.data.endTime)
          if ((nowTime >= startTime) && (nowTime <= endTime)){
            that.setData({
              isoverdue: false
            })
          }
          else{
            that.setData({
              isoverdue: true
            })
          }
          resolve(that.data.isoverdue);
        },
        fail(res){
          reject(res)
        }
      })
      
    })
  },

  OpenLock: function () {
    app.globalData.stopUpgrade = false
    this.getTime().then(res=>{
      console.log(res)
      if (!res){
        this.OpenLock1()
      }
      
    })
  },
  /**
   * @description: 开锁
   * @param {type} 
   * @return: 
   */
  OpenLock1: function (e) {
    var that = this
    if (!that.data.bOpenLock) {
      return
    }
    that.data.bOpenLock = false;
    app.globalData.bletype = 1

    console.log("tmp", app.globalData.bletype)
    var startTime = that.timeTurn(that.data.startTime)
    var endTime = that.timeTurn(that.data.endTime)

    app.globalData.BLE.deviceName = that.data.deviceName
  //  app.globalData.BLE.deviceName = 'WW'
    
    if (app.globalData.BLE.connected) {
      // console.log(startTime, endTime, that.data.pass)
      event.$fire('send-open-event-tmp')
    } else {
      ble.Search()
    }
  },
  /************************** 时间转换 *************************/
  timeTurn: function (time) {
    // console.log(time)
    var time1 = Date.UTC(time[0], time[1] - 1, time[2], time[3] - 8, time[4]);
    time1 = time1 / 1000 / 60
    // console.log(time1)
    return time1
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTime();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    event.$off('ready-event')
    event.$off('send-open-event-tmp')
    event.$off('open-success-event')
    event.$off('open-fail-event')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})